package com.jiyun.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiyun.demo.SendSms;
import com.jiyun.mapper.RechargeMapper;
import com.jiyun.mapper.UserMapper;
import com.jiyun.service.UserService;
import com.reading.pojo.ExchangeVo;
import com.reading.pojo.GlodVO;
import com.reading.pojo.OrderVo;
import com.reading.pojo.Recharge;
import com.reading.pojo.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

import static com.jiyun.demo.EmailVerification.sendMail;

@Transactional
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    RedisTemplate redisTemplate;
    @Override
    public List<User> login(String username, String password) {
        return userMapper.login(username,password);
    }

    @Override
    public String sendcode(String phone) {
        String code = String.valueOf(generateRandomCode());
        redisTemplate.boundValueOps("手机验证码"+phone).set(code,5,TimeUnit.MINUTES);
        try {
            SendSms.main(phone,code);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return code;
    }

    @Override
    public List<User> findPhone(String phone) {
        return userMapper.findPhone(phone);
    }

    @Override
    public List<User> login2(String phone) {
        return userMapper.login2(phone);
    }

    @Override
    public List<User> findEmail(String email) {
        return userMapper.findEmail(email);
    }

    @Override
    public Object sendemail(String email) {
        // 生成四位随机验证码
        String verificationCode = String.valueOf((int) (Math.random() * 9000) + 1000);
        String emailContent = "您的验证码是: " + verificationCode;
        redisTemplate.boundValueOps("邮箱验证码"+email).set(verificationCode, 1, TimeUnit.MINUTES);
        // 发送邮件
        boolean result = sendMail(email, emailContent, "验证码邮件");
        if (result) {
            System.out.println("验证码邮件发送成功");
        } else {
            System.out.println("验证码邮件发送失败");
        }
        return verificationCode;
    }

    @Override
    public List<User> login3(String email) {
        return userMapper.login3(email);
    }

    @Override
    public void add(User user) {
         userMapper.add(user);
    }

    @Override
    public void updatePhone(String userphone, Integer id) {
        userMapper.updatephone(userphone,id);
    }

    @Override
    public void updateEmail(String useremail, Integer id) {
        userMapper.updateEmail(useremail,id);
    }

    @Override
    public void updateUsername(String username, Integer id) {
        userMapper.updateUsername(username,id);
    }

    @Override
    public void updateSex(Integer sex, Integer id) {
        userMapper.updateSex(sex,id);
    }

    @Override
    public List<User> findByInvitation(String invitation) {
        return userMapper.findByInvitation(invitation);
    }

    @Override
    public void updatestatus(Integer id) {
        userMapper.updatestatus(id);
    }

    @Override
    public List<User> find() {
        return userMapper.find();
    }

    @Override
    public void updateimg(String img, Integer id) {
        userMapper.updateimg(img,id);
    }

    @Override
    public User findById(Integer id) {
        return userMapper.findById(id);
    }

    @Override
    public void updatebooks(String books, Integer id) {
        userMapper.updatebooks(books,id);
    }

    @Override
    public PageInfo<User> finduser(User user, Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo,pageSize);
        List<User> userList=userMapper.finduser(user);
        PageInfo<User> pageInfo=new PageInfo<>(userList);
        return pageInfo;
    }

    @Override
    public int xz(Integer a, Integer id) {
        User user = userMapper.findOneUser(id);
        Recharge recharge = new Recharge();
        recharge.setPay(1);
        recharge.setContent("下载小说");
        recharge.setAmount(user.getUserglod1()-a);
        recharge.setPaytime(new Date());
        recharge.setUid(id);
        recharge.setTypename("2");
        rechargeMapper.saveExchange(recharge);
        return userMapper.xz(a,id);
    }

    @Override
    public User findUserById(Integer uid) {
        return userMapper.findUserById(uid);
    }

    @Override
    public void updateHistory(String history, Integer id) {
        userMapper.updateHistory(history,id);
    }


    //生成4为随机数方法
    private static int generateRandomCode() {
        Random random = new Random();
        int randomNumber = random.nextInt(9000) + 1000; // 生成1000到9999之间的随机数
//        redisTemplate.boundValueOps("code_").set(randomNumber, 5, TimeUnit.MINUTES);

        return randomNumber;
    }

    @Override
    public void updateUserGlod1(OrderVo orderVo) {
        userMapper.updateUserGlod1(orderVo);
    }

    @Override
    public User findOneUser(Integer id) {
        return userMapper.findOneUser(id);
    }

    @Override
    public void updateUserGlod2(GlodVO glodVO) {
        userMapper.updateUserGlod2(glodVO);
    }

    @Override
    public void update(int a, Integer id) {
        userMapper.update(a,id);
    }

    @Override
    public void addPurchase(Recharge purchase) {
        userMapper.addPurchase(purchase);
    }

    @Override
    public List<Recharge> findPurchase(Integer uid, Integer pid, Integer chapterId) {
        return userMapper.findPurchase(uid,pid,chapterId);
    }

    @Autowired
    private RechargeMapper rechargeMapper;
    @Override
    public void exchangeGlod(ExchangeVo exchangeVo) {
        User user = userMapper.findOneUser(exchangeVo.getId());
        int gold1 = user.getUserglod1()+exchangeVo.getNumber();
        int gold2 = user.getUserglod2()-exchangeVo.getNumber()*10000;
        exchangeVo.setUsergold1(gold1);
        exchangeVo.setUsergold2(gold2);
        userMapper.exchangeGlod(exchangeVo);

        Recharge recharge = new Recharge();
        recharge.setPay(1);
        recharge.setContent("兑换汪汪币");
        recharge.setAmount(exchangeVo.getNumber());
        recharge.setUid(exchangeVo.getId());
        recharge.setPaytime(new Date());
        recharge.setTypename("3");
        rechargeMapper.saveExchange(recharge);
    }

    @Override
    public void updateVip(Integer id) {
        userMapper.updateVip(id);
    }


}
